/**
* Author: Samfu
* Time  : 上午2:55:47
* Desc  : 
*/
package com.fu.moneybag.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.fu.moneybag.dao.ArticleTypeDao;
import com.fu.moneybag.entity.ArticleType;
import com.fu.moneybag.entity.User;
import com.fu.moneybag.service.ArticleTypeService;
import com.fu.moneybag.validation.UserSessionExpiredChecking;

public class ArticleTypeServiceImpl implements ArticleTypeService {

	private Logger logger = Logger.getLogger(ArticleTypeServiceImpl.class);

	@Autowired
	private ArticleTypeDao articleTypeDao;

	@Autowired
	private UserSessionExpiredChecking sessionChecking;

	@Override
	public List<ArticleType> getAllArticleTypes(long userId) {
		return articleTypeDao.getAllArticleTypes(userId);
	}

	@Override
	public int deleteArticleType(long userId, String articleType) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int createNewArticleType(int userId, String aType) {

		ArticleType articleType = new ArticleType();
		articleType.setArticleType(aType);
		articleType.setUserId(userId);

		return articleTypeDao.createNewArticleType(articleType);
	}

	@Override
	public int deleteArticleTypes(int userId, String[] types) {

		return articleTypeDao.deleteArticleTypes(userId, types);
	}

	@Override
	public String createNewArticleType(HttpServletRequest request, HttpSession session, String articleType) {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info("添加博客类型");
		// here should be a validation
		// session checking for user
		User user = sessionChecking.userSessionCheck(session);
		if (user == null) {
			logger.info("User Session Expired!!");
			return "redirect:/user/login";
		} else {
			session.setAttribute("user", user);
			logger.info("User session valid...");

		}
		logger.info("UserID " + user.getUserId());
		logger.info("ArticleType " + articleType);

		int createArticleTypeResult = this.createNewArticleType(user.getUserId(), articleType);
		if (createArticleTypeResult == 1) {
			session.setAttribute("user", user);
			request.setAttribute("createSucceed", "添加分类成功");
		} else {
			request.setAttribute("createFailed", "添加分类失败");
		}
		List<ArticleType> articleTypes = this.getAllArticleTypes(user.getUserId());

		logger.info("类型总数： " + articleTypes.size());

		// debug check if the array can be pass from the front end
		for (ArticleType a : articleTypes) {
			logger.info("文章类型：" + a.getArticleType());
		}

		request.setAttribute("articleTypes", articleTypes);

		return "articleTypeManagement";
	}

	@Override
	public String getAllArticleTypes(HttpServletRequest request, HttpSession session) {
		logger.info("log for the article type management...");
		
		User user = (User)session.getAttribute("user");
		
		if (user == null){
			return "redirect:/user/login";
		} else {
			session.setAttribute("user", user);
		}
		
		List<ArticleType> articleTypes = this.getAllArticleTypes(user.getUserId());
		
		logger.info("类型总数： " + articleTypes.size());
		
		//debug check if the array can be pass from the front end
		for (ArticleType a: articleTypes){
			logger.info("文章类型：" + a.getArticleType());
		}
		
		request.setAttribute("articleTypes", articleTypes);
		
		return "articleTypeManagement";
	}

	@Override
	public String deleteArticleTypes(HttpServletRequest request, HttpSession session, String[] types) {
		
		User user = (User)session.getAttribute("user");
		if (user == null) {
			return "redirect: /user/login";
		}
		logger.info("User id " +user.getUserId());
		logger.info("array length " + types.length);
		
		for (int i=0; i<types.length; i++) {
			logger.info("要删除的record " + types[i]);
		}
		
		int result = this.deleteArticleTypes(user.getUserId(), types);
		
		logger.info("删除数目： " + result);
		
		if (result >0) {
			request.setAttribute("deleteSucceed", "删除成功！");
		} else {
			request.setAttribute("deleteFailed", "删除失败！");
		}

		session.setAttribute("user", user);
		
		List<ArticleType> articleTypes = this.getAllArticleTypes(user.getUserId());
		
		logger.info("类型总数： " + articleTypes.size());
		
		//debug check if the array can be pass from the front end
		for (ArticleType a: articleTypes){
			logger.info("文章类型：" + a.getArticleType());
		}
		
		request.setAttribute("articleTypes", articleTypes);
		return "articleTypeManagement";
	}

	@Override
	public List<HashMap<String, Integer>> getArticleGroupByType(int userId) {
		return articleTypeDao.getArticleGroupByType(userId);
	}

}
